package com.k2data.k2app.controller;

import com.k2data.k2app.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @Author cuilibo@k2data.com.cn
 * @Date 17-12-27 下午2:57.
 */
@Api(description = "redis")
@RestController
@RequestMapping("/redis")
public class TestWebController {
    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "打印测试")
    @GetMapping(value = "/echo")
    public String echo() {
        return "Hello init Docker!!!!!!!!!!!!!!!!!!!";
    }
    @ApiOperation(value = "获取key的value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping(value = "/redis/get/{key}")
    public String rGet(@PathVariable("key") String key) {
        Object vString = redisService.get(key);
        System.out.println("redisService:" + vString);
        return "Redis Value:" + vString;
    }
    @ApiOperation(value = "设置key的value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "Long", paramType = "path")
    })
     @GetMapping(value = "/redis/set/{key}/{value}")
    public String rSet(@PathVariable("key") String key, @PathVariable("value") String value) {
        redisService.set(key, value);
        System.out.println("redisService:  SET OK");
        return "Redis Value: SET OK key:" + key + ",value:" + value;
    }

    @ApiOperation(value = "设置key的value,value是object类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "Object", paramType = "body")
    })
    @PostMapping(value = "/redis/set/{key}",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String rSetObject(@PathVariable("key") String key, @RequestBody Object value) {
        redisService.set(key, value);
        System.out.println("redisService:  SET OK");
        return "Redis Value: SET OK key:" + key + ",value:" + value;
    }
}
